import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-05-16
 * Time: 19:25
 */
public class Test {

    //投票法
    public int majorityElement(int[] array) {
        int ret = array[0];
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] == ret){
                count++;
            }else{
                count--;
            }
            if(count == 0){
                ret = array[i+1];
            }
        }
        return ret;
    }

    public static boolean func4(int[] array){
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] % 2 != 0){//为奇数
                count++;
                if(count == 3){//判断是否有3个奇数
                    return true;
                }
            }else {//判断是否有连续的3个奇数
                count = 0;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 2, 1};
        System.out.println(func4(array));
    }

    public static int func3(int[] array){
        int ret = array[0];
        for (int i = 1; i < array.length; i++) {
            ret = ret ^ array[i];
        }
        return ret;
    }

    public static int[] func2(int[] array, int target){
        int[] ret = new int[2];
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] + array[j] == target){
                    ret[0] = i;
                    ret[1] = j;
                    return ret;
                }
            }
        }
        return ret;
    }

    //i遇到偶数停止   j遇到奇数停止
    //二者进行交换
    public static void func1(int[] array){
        int i = 0;
        int j = array.length-1;
        while(i < j){
            while(i < j && array[i] % 2 != 0){//为奇数
                i++;
            }
            while(i < j && array[j] % 2 == 0){//为偶数
                j--;
            }
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }

    public static void main6(String[] args) {
        int[] array = {1,2,3,4,5,6};
        func1(array);
        System.out.println(Arrays.toString(array));
    }

    public static void transform1(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }

    public static int[] transform(int[] array){
        int[] tmpArr = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            tmpArr[i] = array[i] * 2;
        }
        return tmpArr;
    }

    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] ret = transform(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void main4(String[] args) {
        //不规则的数组
        int[][] array1 = new int[2][];
        array1[0] = new int[3];
        array1[1] = new int[4];

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();

        System.out.println(Arrays.deepToString(array1));

    }

    public static void main3(String[] args) {
        int[][] array1 = new int[2][3];
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();


        for (int[] arr : array1) {
            for(int x : arr){
                System.out.print(x+" ");
            }
            System.out.println();
        }


        System.out.println();

        System.out.println(Arrays.deepToString(array1));

        /*System.out.println(array1[0]);
        System.out.println(Arrays.toString(array1[0]));
        System.out.println(array1[1]);
        System.out.println(Arrays.toString(array1[1]));*/

    }

    public static void main2(String[] args) {
        //2行3列的二维数组
        int[][] array1 = new int[2][3];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println();

        System.out.println(Arrays.deepToString(array1));

    }

    public static void main1(String[] args) {

        int[][] array1 = new int[2][3];//默认初始化值都为0

        int[][] array2 = new int[][]{{1,2,3},{4,5,6,}};

        int[][] arrays = {{1,2,3},{4,5,6}};
    }
}
